home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / UCRASM25.ARJ / STRREVM.ASM < prev    next >
Assembly Source File  |  1991-10-12  |  1KB  |  93 lines

  1. StdGrp        group    stdlib,stddata
  2. stddata        segment    para public 'sldata'
  3. stddata        ends
  4. ;
  5. stdlib        segment    para public 'slcode'
  6.         assume    cs:stdgrp
  7. ;
  8.         extrn    sl_malloc:far
  9. ;
  10. ;
  11. ; strrevm- reverses the characters in a string.
  12. ;
  13. ; inputs:
  14. ;
  15. ;    ES:DI- Points at the string to reverse.
  16. ;
  17. ; outputs:
  18. ;
  19. ;    ES:DI- Points at new string on the heap.
  20. ;    Carry=1 if memory allocation error, 0 if no error.
  21. ;
  22. ;
  23. ; Created by Mike Blaszczak (.B ekiM)  8/8/90
  24. ; Some minor tweaking by R. Hyde 8/9/90
  25. ;
  26. ;
  27.         public    sl_strrevm
  28. ;
  29. ;
  30. sl_strrevm    proc    far
  31.         push    ds
  32.         push    si
  33.         push    ax
  34.         push    cx
  35.         pushf
  36.         cld
  37. ;
  38. ;
  39. ; Compute the length (+1 for zero byte) of the string:
  40. ;
  41.         mov    cx, 0ffffh
  42.         mov    al, 0
  43.     repne    scasb
  44.         neg    cx
  45.         dec    cx
  46. ;
  47. ; Save ptr to end of the string
  48. ;
  49.         mov    si, es
  50.         mov    ds, si
  51.         lea    si, (0-1)[di]        ;Points at zero byte.
  52. ;
  53. ; Allocate storage for the new string:
  54. ;
  55.         mov    ax, cx            ;Save length
  56.         call    sl_malloc
  57.         mov    cx, ax            ;Restore length
  58.         jc    BadStrRev
  59.         push    es            ;Save ptr to string.
  60.         push    di
  61. ;
  62. ; Note that string length is always at least one (for the zero byte).
  63. ; Copy and reverse the string down here.
  64. ;
  65. CopyBytes:    dec    si
  66.         mov    al, [si]
  67.         stosb
  68.         loop    CopyBytes
  69.         mov    byte ptr es:[di], 0
  70.         pop    di            ;Restore ptr to new string.
  71.         pop    es
  72. ;
  73.         popf
  74.         pop    cx
  75.         pop    ax
  76.         pop    si
  77.         pop    ds
  78.         clc
  79.         ret
  80. ;
  81. BadStrRev:    popf
  82.         pop    cx
  83.         pop    ax
  84.         pop    si
  85.         pop    ds
  86.         stc
  87.         ret
  88. sl_strrevm    endp
  89. ;
  90. ;
  91. stdlib        ends
  92.         end
  93.